﻿using System.Application;
using System.Collections.Generic;
using System.Domain.Entity;
using System.Domain.IRepository;
using System.Linq;
using System.Repository;
using Application.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Ubiquity.Core.Extend;
using Ubiquity.Core.Web;
using Ubiquity.Core.Web.Tree;
using Ubiquity.Web.Filters;

namespace Ubiquity.Web.Areas.SystemManage.Controllers
{

    [Area("SystemManage")]
    public class SysUserController : BaseController
    {
       
        private readonly SysUserService _service;
        public SysUserController(SysUserService service)
        {
            _service = service;
        }
        public IActionResult GetGridPaginationJson(WebPagination pagination, string companyId, string departmentId, string positionId, string userId)
        {
            var paging = pagination.Change();
            var data = new
            {
                rows = _service.GetList(paging, companyId, departmentId, positionId, userId),
                total = paging.Total,
                page = paging.Page + 1,
                records = paging.Records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [Authorize]
        public IActionResult GetDictionaryJson()
        {
            var data = _service.GetList();
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (var item in data)
            {
                var fieldItem = new
                {
                    encode = item.Account,
                    fullname = item.RealName
                };
                dictionary.Add(item.Id, fieldItem);
            }
            return Content(dictionary.ToJson());
        }


        /// <summary>
        /// 得到  Select
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTreeSelectBusinessJson()
        {

            //   PayBusinessService app = new PayBusinessService();
            var data = _service.GetList();
            var treeList = data.Select(item => new TreeSelectModel
            {
                id = item.Id.ToString(),
                text = item.RealName,
                parentId = "0"
            }).ToList();
            return Content(treeList.TreeSelectJson());
        }


        [HttpGet]
        public IActionResult GetFormJson(string keyValue)
        {
            var data = _service.GetEntity(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public IActionResult Insert(SysUserEntity entity, string keyValue)
        {
            return _service.Insert(entity) ? Success("操作成功。") : Error("操作失败。");
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public IActionResult Update(SysUserEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Id = keyValue;
                return _service.Update(entity) ? Success("操作成功。") : Error("操作失败。");
            }
            return Error("主键验证错误");
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public IActionResult DeleteForm(string keyValue)
        {
            return _service.Delete(keyValue) ? Success("操作成功。") : Error("操作失败。");
        }

        [HttpGet]
        [Authorize]
        public IActionResult RevisePassword()
        {
            return View();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userPassword"></param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public IActionResult SubmitRevisePassword(string userPassword, string keyValue)
        {
            _service.RevisePassword(keyValue, userPassword);
            return Success("重置密码成功。");
        }
    }
}